#include "swpjoingraphicitem.h"

#include "graphicitemcommon.h"

#include <QtGui/QGraphicsScene>

using namespace core;
using namespace swp;

SwpJoinGraphicItem::SwpJoinGraphicItem(
    QGraphicsItem* parent) : AbstractPolygonGraphicItem(parent)
{
    QPolygonF polygon;
    polygon << QPointF(0.0, 0.0) << QPointF(135.0, 0.0) <<
        QPointF(135.0, 25.0) << QPointF(0.0, 25.0) << QPointF(0.0, 0.0);

    setPolygonF(polygon);

    setFlags(flags() | QGraphicsItem::ItemIsMovable |
        QGraphicsItem::ItemIsSelectable);

    setAntialiasing(false);

    setPen(QPen(Qt::transparent, 0));
    setBrush(QBrush(Qt::black, Qt::SolidPattern));
}

SwpJoinGraphicItem::~SwpJoinGraphicItem()
{

}

bool SwpJoinGraphicItem::supportsConnection(
    const ConnectionData& connection) const
{
    bool ret = false;

    QList<ConnectionData> _connections = connections();

    if (connection.ConnectionId != CONNECTION_NOT_VALID &&
        !containsSameConnection(connection, _connections))
    {
        const bool isIncoming = connection.To == this;

        if (isIncoming)
        {
            const int incomingSize = incomingConnectionSize(_connections);

            ret = incomingSize < 2;
        }
        else
        {
            const int outgoingSize = outgoingConnectionSize(_connections);

            ret = outgoingSize < 1;
        }
    }

    return ret;
}

int SwpJoinGraphicItem::type() const
{
    return SWP_JOIN_ITEM;
}

QString SwpJoinGraphicItem::name() const
{
    return QObject::tr("Join");
}

IEditorItem* SwpJoinGraphicItem::copy() const
{
    return new SwpJoinGraphicItem(0);
}

QList<Property> SwpJoinGraphicItem::properties() const
{
    QList<Property> ret;

    return ret;
}

QVariant SwpJoinGraphicItem::propertyValue(
    const QString& propertyName) const
{
    QVariant ret;

    return ret;
}

void SwpJoinGraphicItem::propertyChanged(const QString& property,
    const QVariant& newValue, bool silent)
{

}

QVariantList SwpJoinGraphicItem::optionList(
    const QString& propertyName) const
{
    QVariantList ret;

    return ret;
}

QVariantList SwpJoinGraphicItem::autocomplete(
    const QString& propertyName) const
{
    QVariantList ret;

    return ret;
}

QList<QPointF> SwpJoinGraphicItem::scalePoints() const
{
    QList<QPointF> ret;

    ret << QPointF(67.5, 0.0) << QPointF(67.5, 25)
        << QPointF(0.0, 12.5) << QPointF(135, 12.5);

    return ret;
}

QList<QPointF> SwpJoinGraphicItem::connectPoints() const
{
    QList<QPointF> ret;

    ret << QPointF(35.0, 0.0) << QPointF(100.0, 0.0) << QPointF(67.5, 25.0);

    return ret;
}

QList<QPointF> SwpJoinGraphicItem::connectPointsForIncoming() const
{
    QList<QPointF> ret;

    ret << QPointF(35.0, 0.0) << QPointF(100.0, 0.0);

    return ret;
}

QList<QPointF> SwpJoinGraphicItem::connectPointsForOutgoing() const
{
    QList<QPointF> ret;

    ret << QPointF(67.5, 25.0);

    return ret;
}

QList<QPointF> SwpJoinGraphicItem::selfConnectAnchorPoints() const
{
    QList<QPointF> ret;

    ret << QPointF(-20.0, -20.0) << QPointF(35.0, -20.0) <<
        QPointF(100.0, -20.0) << QPointF(155.0, -20.0) << QPointF(155.0, 45.0)
        << QPointF(67.5, 45.0) << QPointF(-20.0, 45.0);

    return ret;
}

QList<int> SwpJoinGraphicItem::selfConnectAnchorMappings(
    ) const
{
    QList<int> ret;

    ret << 1 << 2 << 5;

    return ret;
}

int SwpJoinGraphicItem::paletteType() const
{
    return Whole;
}

bool SwpJoinGraphicItem::containsSameConnection(
    const ConnectionData& connection,
    const QList<ConnectionData>& connections) const
{
    bool ret = false;

    foreach (const ConnectionData& _connection, connections)
    {
        if (ConnectionComparator::compare(connection, _connection,
            ConnectionComparator::WithoutConnector))
        {
            ret = true;
            break;
        }
    }

    return ret;
}

int SwpJoinGraphicItem::outgoingConnectionSize(
    const QList<ConnectionData>& connections) const
{
    int ret = 0;

    foreach (const ConnectionData& _connection, connections)
    {
        if (_connection.From == this)
        {
            ++ret;
        }
    }

    return ret;
}

int SwpJoinGraphicItem::incomingConnectionSize(
    const QList<ConnectionData>& connections) const
{
    int ret = 0;

    foreach (const ConnectionData& _connection, connections)
    {
        if (_connection.To == this)
        {
            ++ret;
        }
    }

    return ret;
}
